package demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 文件读写
 * Created by yezhengle on 2017/7/26.
 * scanner读取
 * FileWriter
 */
public class D_19 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D://data.dat");
        
        Scanner scanner = new Scanner(fileInputStream);
        List<Double> data = new ArrayList<Double>();
        while(scanner.hasNextDouble()){
        	double dataD = scanner.nextDouble();
//        	System.out.println(dataD);
        	data.add(dataD);
        }
        fileInputStream.close();
        
        Collections.sort(data);
        double sum = 0;
        for(double d: data){
        	sum += d;
        	System.out.println(d);
        }
        
        
        File file = new File("D://result.text");
        if(file.exists()){
        	file.delete();
        }
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write("选手编号:201601XX\r\n");
        fileWriter.write("数据个数:"+data.size()+"\r\n");
        fileWriter.write("最大:"+data.get(data.size()-1)+"\r\n");
        fileWriter.write("最小:"+data.get(0)+"\r\n");
        fileWriter.write("平均:"+sum/data.size()+"\r\n");
        fileWriter.flush();
        fileWriter.close();
    }
}
